﻿using System;
using System.Drawing;
using Finance.Data.Entities;
using Finance.Properties;
using TodoSoft;
using TodoSoft.Reporting;

namespace Finance.Reports
{
	internal class IncomeExpenseReport : Report
	{
		public IncomeExpenseReport(Action<Guid> openDocumentAction)
		{
			DataQuery = "select document.documentid as documentid, " +
			            "document.docdatetime as docdatetime, " +
			            "document.description as description, " +
			            "document.amount as amount, " +
						"document.amount * 30.5 / (DATEDIFF(DAY, @Period_start, @Period_end) + 1) as monthamount, " +
			            "document.typecode as documenttypecode, " +
			            "case document.typecode " +
			            "when " + (int)DocumentType.Rashod + " then '" + Resources.DocumentType_Expense + "' " +
			            "else '" + Resources.DocumentType_Income + "' end as documenttypename, " +
			            "documentsubtype.documentsubtypeid as categoryid, " +
			            "documentsubtype.name as categoryname " +
			            "from document " +
			            "left join documentsubtype on documentsubtype.documentsubtypeid = document.documentsubtypeid " +
			            "where document.docdatetime >= @Period_start and document.docdatetime <= @Period_end " +
						"order by documentsubtype.name, document.docdatetime";

			// Document type grouping
			var documentTypeGroup = new ReportGrouping("documenttypecode") { Font = new Font("Arial", 10, FontStyle.Bold)};
			documentTypeGroup.Fields.Add(ReportDataField.First("documenttypename", 100, Resources.DocumentsReportControl_DocumentTypeColumn));

			var documentTypeAmountField = ReportDataField.Sum("amount", 60, Resources.DocumentsReportControl_SumColumn);
			documentTypeAmountField.ShrinkToFit = true;
			documentTypeAmountField.FormatArgument = "C";
			documentTypeAmountField.StringFormat = new StringFormat {Alignment = StringAlignment.Far};
			documentTypeGroup.Fields.Add(documentTypeAmountField);

			var documentTypeMonthAmountField = ReportDataField.Sum(
				"monthamount", 55, Resources.DocumentsReportControl_MonthSumColumn);
			documentTypeMonthAmountField.ShrinkToFit = true;
			documentTypeMonthAmountField.FormatArgument = "C";
			documentTypeMonthAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			documentTypeGroup.Fields.Add(documentTypeMonthAmountField);
			Groupings.Add(documentTypeGroup);

			// Product category grouping
			var productCategoryGroup = new ReportGrouping("categoryid") { HeaderRowVisible = false, Expanded = false };
			productCategoryGroup.Fields.Add(ReportDataField.First(
                "categoryname", 100, Resources.DocumentsReportControl_ProductCategoryColumn));

			var categoryAmountField = ReportDataField.Sum("amount", 60, Resources.DocumentsReportControl_SumColumn);
			categoryAmountField.ShrinkToFit = true;
			categoryAmountField.FormatArgument = "C";
			categoryAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			productCategoryGroup.Fields.Add(categoryAmountField);

			var categoryMonthAmountField = 
				ReportDataField.Sum("monthamount", 55, Resources.DocumentsReportControl_MonthSumColumn);
			categoryMonthAmountField.ShrinkToFit = true;
			categoryMonthAmountField.FormatArgument = "C";
			categoryMonthAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			productCategoryGroup.Fields.Add(categoryMonthAmountField);
			Groupings.Add(productCategoryGroup);

			// Document grouping
			var documentGroup = new ReportGrouping("documentid") {HeaderRowVisible = false};
			var documentDateField = 
				ReportDataField.First("docdatetime", 65, Resources.DocumentsReportControl_DateDocumentColumn);
			documentDateField.Padding = new Margins(15, 0, 0, 0);
			if(openDocumentAction != null)
				documentDateField.CellClick += data => openDocumentAction((Guid) data["documentid"]);
			documentGroup.Fields.Add(documentDateField);
			documentGroup.Fields.Add(ReportDataField.First("description", 80, Resources.DocumentsReportControl_DescriptionColumn));

			var documentAmountField = ReportDataField.First("amount", 80, Resources.DocumentsReportControl_SumColumn);
			documentAmountField.FormatArgument = "C";
			documentAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			documentGroup.Fields.Add(documentAmountField);
			Groupings.Add(documentGroup);

			Parameters.Add(new PeriodReportParameter("Period")
				{
					MinValueQuery = "select min(docdatetime) from document",
					MaxValueQuery = "select max(docdatetime) from document"
				});
		}
	}
}
